package io.renren.sf.controller;

import com.hz.common.utils.ResponseData;
import com.hz.utils.FixedInformationName;
import io.renren.sf.entity.HzFixedInformationDicEntity;
import io.renren.sf.service.HzFixedInformationDicService;
import io.renren.sf.vo.CopyingBackgroundMusicVo;
import io.renren.sf.vo.CopyingCharacterLatticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 公共音乐、公共排版设置
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2023-05-11 17:06:11
 */
@RestController
@RequestMapping("sf/copyingPublicSetting")
@Api(tags = "临摹公共设置")
public class CopyingPublicSettingController {

    @Autowired
    private HzFixedInformationDicService fixedInformationDicService;

    /**
     * 公共字格列表删除
     */
    @ApiOperation("公共字格列表删除")
    @PostMapping("/characterLattice/delete")
    public ResponseData characterLatticeDelete(@RequestBody Long[] ids){
        fixedInformationDicService.removeByIds(Arrays.asList(ids));
        return ResponseData.success("删除成功");
    }

    /**
     * 公共字格获取
     */
    @ApiOperation("公共字格获取")
    @GetMapping("/characterLattice/get")
    public ResponseData<CopyingCharacterLatticeVo> characterLatticeGet(){
        List<HzFixedInformationDicEntity> fixedInformationDicList = fixedInformationDicService.queryByName(FixedInformationName.CO_CHARACTER_LATTICE);
        List<CopyingCharacterLatticeVo> characterLatticeList = fixedInformationDicList.stream()
                .map(fixedInformationDic -> new CopyingCharacterLatticeVo(fixedInformationDic.getId(),
                        fixedInformationDic.getRemarks(),
                        fixedInformationDic.getFixedInformationContent(),
                        fixedInformationDic.getUpdatetime()))
                .collect(Collectors.toList());
        return ResponseData.success(characterLatticeList);
    }

    /**
     * 公共字格保存
     */
    @ApiOperation("公共字格保存")
    @PostMapping("/characterLattice/save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "字格名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "characterLatticeSrc", value = "字格src", paramType = "query", dataType = "String", required = true)
    })
    public ResponseData characterLatticeSave(@RequestParam("name") String name,
                                             @RequestParam("characterLatticeSrc") String characterLatticeSrc){
        Integer nums = fixedInformationDicService.queryByNameAndRemarksNum(FixedInformationName.CO_CHARACTER_LATTICE, name);
        if (nums >= 1){
            return ResponseData.success("保存失败，该记录只能存在唯一字格名称！",false);
        }
        HzFixedInformationDicEntity fixedInformationDic = new HzFixedInformationDicEntity(FixedInformationName.CO_CHARACTER_LATTICE, characterLatticeSrc, new Date(), name);
        fixedInformationDicService.save(fixedInformationDic);
        return ResponseData.success("保存成功");
    }

    /**
     * 公共字格修改
     */
    @ApiOperation("公共字格修改")
    @PostMapping("/characterLattice/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字格id", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "name", value = "字格名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "characterLatticeSrc", value = "字格src", paramType = "query", dataType = "String", required = true)
    })
    public ResponseData characterLatticeUpdate(@RequestParam("id") Long id,
                                               @RequestParam("name") String name,
                                               @RequestParam("characterLatticeSrc") String characterLatticeSrc){
        HzFixedInformationDicEntity fixedInformationDic = new HzFixedInformationDicEntity(id, characterLatticeSrc, new Date(), name);
        fixedInformationDicService.updateById(fixedInformationDic);
        return ResponseData.success("修改成功");
    }


    /**
     * 公共背景音乐获取
     */
    @ApiOperation("公共背景音乐获取")
    @GetMapping("/backgroundMusic/get")
    public ResponseData<CopyingBackgroundMusicVo> backgroundMusicGet(){
        List<HzFixedInformationDicEntity> musicList = fixedInformationDicService.queryByName(FixedInformationName.CO_BACKGROUND_MUSIC);
        if (musicList.size() == 0)
            return ResponseData.success("还未上传背景音乐！");
        HzFixedInformationDicEntity fixedInformationDic = musicList.get(0);
        CopyingBackgroundMusicVo music = new CopyingBackgroundMusicVo(
                fixedInformationDic.getId(),
                fixedInformationDic.getFixedInformationContent(),
                fixedInformationDic.getUpdatetime()
        );
        return ResponseData.success(music);
    }

    /**
     * 公共背景音乐保存
     */
    @ApiOperation("公共背景音乐保存")
    @PostMapping("/backgroundMusic/save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "music", value = "背景音乐src", paramType = "query", dataType = "String", required = true)
    })
    public ResponseData backgroundMusicSave(@RequestParam("music") String music){
        Integer nums = fixedInformationDicService.queryFixedInformationDicsNum(FixedInformationName.CO_BACKGROUND_MUSIC);
        if (nums >= 1){
            return ResponseData.success("保存失败，该记录只能存在唯一背景音乐！",false);
        }
        HzFixedInformationDicEntity fixedInformationDic = new HzFixedInformationDicEntity(FixedInformationName.CO_BACKGROUND_MUSIC, music, new Date(), "临摹系统背景音乐");
        fixedInformationDicService.save(fixedInformationDic);
        return ResponseData.success("保存成功");
    }

    /**
     * 公共背景音乐修改
     */
    @ApiOperation("公共背景音乐修改")
    @PostMapping("/backgroundMusic/update")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "背景音乐id", paramType = "query", dataType = "Long", required = true),
            @ApiImplicitParam(name = "music", value = "背景音乐src", paramType = "query", dataType = "String", required = true)
    })
    public ResponseData backgroundMusicUpdate(@RequestParam("id") Long id,
                                              @RequestParam("music") String music){
        HzFixedInformationDicEntity fixedInformationDic = new HzFixedInformationDicEntity(id, music, new Date());
        fixedInformationDicService.updateById(fixedInformationDic);
        return ResponseData.success("修改成功");
    }

}
